package cn.ffcs.uec.wBStaffInfo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecDocEduManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecDocEdu;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbDocEduListboxBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * @desc :教育经历 CRUD插件
 * @author BMP
 * @time 2019-01-05
 */
@Controller
@Scope("prototype")
public class WbDocEduMainComposer  extends BasePortletComposer implements
IPortletInfoProvider {

	private static final long serialVersionUID = -5811398149406451671L;
	private String UEC_OUT_DOC_EDU_EDIT_ZUL="/pages_nmg/uecOutStaffInfomation/uec_out_doc_edu_edit.zul";
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo = new UecOutStaffInfo();
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private WbDocEduListboxBean bean = new WbDocEduListboxBean();
	/**
	 * 教育经历实体
	 */
	private UecDocEdu uecDocEdu;
	/**
	 * 翻页查询条件实体
	 */
	private UecDocEdu docEduPaging;

	//private UecDocEdu approvalDocEduPaging;
	/**
	 * 入职页面添加的教育经历列表
	 */
	private List<UecDocEdu> eduList = new ArrayList<UecDocEdu>();

	private UecDocEduManager uecEduManager = (UecDocEduManager) ApplicationContextUtil
			.getBean("uecDocEduManager");
	private UecOutStaffManager uecOutStaffInfoManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);
		Components.wireVariables(comp, bean);
	}


	/**
	 * 初始化
	 */
	public void onCreate$wbMainWin() {
		//this.setButtonValid(false, false, false, false);
		onUecOutDocEduQueryResponse();
	
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	// onSelectUecOutDocEduList
	public void onSelectUecOutDocEduList() throws Exception {
		if (this.bean.getUecOutDocEduListbox().getSelectedCount() > 0) {
			uecDocEdu = (UecDocEdu) bean.getUecOutDocEduListbox()
					.getSelectedItem().getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutDocEduQueryResponse() {
		//uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户不存在!", "错误提示");
			return;
		} else {
			String screenName = PlatformUtil.getCurrentUser().getScreenName();
			uecOutStaffInfo.setPsnCode(screenName.toUpperCase());
		}
		if(uecOutStaffInfo!=null) {
			uecOutStaffInfo=uecOutStaffInfoManager.findUecOutStaffInfo(uecOutStaffInfo);
		}
		if (null != uecOutStaffInfo) {
			uecDocEdu = new UecDocEdu();
			uecDocEdu.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecDocEdu.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			docEduPaging = uecDocEdu;			
			this.onQueryUecDocEdu();
		}
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecDocEdu() {
		this.bean.getUecOutDocEduListboxPaging().setActivePage(0);
		this.onQueryUecOutDocEduList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecDocEdu() {
		ListboxUtils.clearListbox(this.bean.getUecOutDocEduListbox());
		this.bean.getUecOutDocEduListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void onViewUecOutDocEdu() throws SuspendNotAllowedException,
			Exception {
		if (uecDocEdu != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecDocEdu", uecDocEdu);
			Window window = (Window) Executions.createComponents(
					this.UEC_OUT_DOC_EDU_EDIT_ZUL, null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
			return;
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecOutDocEdu() throws Exception {
		if (null != uecOutStaffInfo) {
				boolean isOnTrial = isOnTrial();
				if (!isOnTrial) {
					return;
				}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(
					this.UEC_OUT_DOC_EDU_EDIT_ZUL, null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecDocEdu=null;
							onQueryUecOutDocEduList();
					}
				}
			});
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecOutDocEdu() throws Exception, InterruptedException {
		if (uecDocEdu != null) {
			boolean isOnTrial = isOnTrial();
			if (!isOnTrial) {
				return;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecDocEdu", uecDocEdu);
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(
					this.UEC_OUT_DOC_EDU_EDIT_ZUL, null, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						uecDocEdu=null;
						// 新增或修改完成后，列表从数据库更新
							onQueryUecOutDocEduList();

					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecOutDocEdu() {
		if (this.uecDocEdu != null && this.uecDocEdu.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecDocEdu.removeOnly();
						// PubUtil.reDisplayListbox(bean.getUecOutDocEduListbox(),
						// uecDocEdu, "del");
						onQueryUecOutDocEduList();
						uecDocEdu = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutDocEduListboxPaging() {
		this.onQueryUecOutDocEduList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecDocEdu = null;
		docEduPaging = null;
		eduList.clear();
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutDocEduList() {
		// 翻页查看生效数据
		if (null != docEduPaging) {
			PageInfo pageInfo = uecEduManager.queryPageInfoByUecDocEdu(
					docEduPaging, this.bean.getUecOutDocEduListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecOutDocEduListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecOutDocEduListbox().setModel(list);
			this.bean.getUecOutDocEduListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}
	/**
	 * 判断是否入职
	 * 
	 * @return
	 */
	private boolean isOnTrial() {
		UecTrial qryUecTrial = UecTrial.newInstance();
		qryUecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
		qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
		if (null == qryUecTrial) {
			ZkUtil.showError("您未办理入职,请主管执行入职操作", "温馨提示");
			return false;
		}
		return true;
	}
}
